package com.android.nfctouch.ui.fragment;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.android.baselibrary.dialog.Comm_DialogFragment;
import com.android.baselibrary.event.Event_Token_Failure;
import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.ui.BaseFragment;
import com.android.baselibrary.util.GlideUtil;
import com.android.baselibrary.view.Comm_TitleView;
import com.android.baselibrary.view.MultiStateView;
import com.android.nfctouch.R;
import com.android.nfctouch.bean.UserInfo;
import com.android.nfctouch.constants.URLConstants;
import com.android.nfctouch.manager.API_Manager;
import com.android.nfctouch.manager.UserManager;
import com.android.nfctouch.ui.activity.AboutUsActivity;
import com.android.nfctouch.ui.activity.BindActivationCodeActivity;
import com.android.nfctouch.ui.activity.EditUserInfoActivity;
import com.android.nfctouch.ui.activity.WebViewActivity;
import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeMineFragment extends BaseFragment {

    @BindView(R.id.iv_img_bg)
    ImageView iv_img_bg;
    @BindView(R.id.ll_title_bar)
    LinearLayout ll_title_bar;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_remark)
    TextView tv_remark;
    @BindView(R.id.ctv_bindjihuoma)
    Comm_TitleView ctv_bindjihuoma;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this).titleBar(ll_title_bar).statusBarDarkFont(true).navigationBarDarkIcon(true).init();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mContext != null) {
            getUserInfo();
        }
    }

    @Override
    protected void initData() {

    }

    //获取用户信息
    private void getUserInfo() {
        API_Manager.userInfo(mContext, new OkHttpCallBack<BaseResponce<UserInfo>>() {
            @Override
            public void onSuccess(BaseResponce<UserInfo> baseResponce) {
                CommLoading.dismissLoading();
                UserManager.saveUserInfo(mContext, baseResponce.getData());
                bindUserInfo();
            }

            @Override
            public void onFailure(BaseResponce<UserInfo> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, baseResponce.msg);
            }
        });
    }

    //刷新用户信息
    private void bindUserInfo() {
        UserInfo userInfo = UserManager.getUserInfo(mContext);
        if (userInfo != null) {
            GlideUtil.displayImage(mContext, userInfo.avatarUrl, iv_head, -1);
            GlideUtil.displayImage(mContext, userInfo.backgroundUrl, iv_img_bg, -1);
            tv_nickname.setText(userInfo.username);
            if (!TextUtils.isEmpty(userInfo.remark)) {
                tv_remark.setText(userInfo.remark);
            } else {
                tv_remark.setText("暂无简介...");
            }
            if (!TextUtils.isEmpty(userInfo.activationCode)) {
                ctv_bindjihuoma.setVisibility(View.GONE);
            }


        }
    }

    @OnClick({R.id.ctv_edit_userinfo, R.id.ctv_yonghuxieyi, R.id.ctv_sign_out, R.id.ctv_aboutus, R.id.ctv_yijianfankui, R.id.ctv_bindjihuoma,R.id.csb_loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ctv_edit_userinfo:    //编辑资料
                StartActivity(EditUserInfoActivity.class);
                break;
            case R.id.ctv_yonghuxieyi:  //用户协议
                WebViewActivity.startActivity(mContext, "", URLConstants.URL_xieyi1);
                break;
            case R.id.ctv_sign_out:  //注销
                Comm_DialogFragment.showCommDialog(getActivity(), "", "确定注销账号?", "确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        user_logoff();
                    }
                }, null);
                break;
            case R.id.ctv_aboutus:  //关于我们
                StartActivity(AboutUsActivity.class);
                break;
            case R.id.ctv_yijianfankui:  //意见反馈

                break;
            case R.id.ctv_bindjihuoma:  //绑定激活码
                StartActivity(BindActivationCodeActivity.class);
                break;
            case R.id.csb_loginout:
                Comm_DialogFragment.showCommDialog((FragmentActivity) mContext, "", "确定退出登录?","确定", "取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EventBus.getDefault().post(new Event_Token_Failure());
                    }
                }, null);
                break;
        }
    }

    //注销账号
    private void user_logoff() {
        API_Manager.user_update(mContext, "lockFlag", "1", new OkHttpCallBack<BaseResponce<String>>() {
            @Override
            public void onSuccess(BaseResponce<String> baseResponce) {
                CommToast.showToast(mContext,"注销成功");
                EventBus.getDefault().post(new Event_Token_Failure());
            }

            @Override
            public void onFailure(BaseResponce<String> baseResponce) {
                CommToast.showToast(mContext,"注销失败");
            }
        });
    }

}
